﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;

#if UNITY_EDITOR
using UnityEditor;
#endif

public class MainConfigPath
{
    //获取当前应用程序所在目录
    //Environment.CurrentDirectory
    //E:\U3dProj\borderExtension\BorderExpansion\borderExpansionProj

    //Application.streamingAssetsPath
    //E:/U3dProj/borderExtension/BorderExpansion/borderExpansionProj/Assets/StreamingAssets


    //public static string dllPath = Application.dataPath.Replace("Assets", "") + "Library/ScriptAssemblies/Assembly-CSharp.dll";
    //public static string pdbPath = Application.dataPath.Replace("Assets", "") + "Library/ScriptAssemblies/Assembly-CSharp.pdb";

    //pc路径
    //public static string dllPath = Environment.CurrentDirectory.Replace("\\", "/") + "/Library/ScriptAssemblies/Assembly-CSharp.dll";
    //public static string pdbPath = Environment.CurrentDirectory.Replace("\\", "/") + "/Library/ScriptAssemblies/Assembly-CSharp.pdb";

    //各平台路径
    public static string dllPath = StreamingAssetsPath() + "Assembly-CSharp.dll";
    public static string pdbPath = StreamingAssetsPath() + "Assembly-CSharp.pdb";
    

    //C:/Users/Administrator/AppData/LocalLow/DefaultCompany/borderExpansionProj/AssetBundles/
    //public static string DownLoadABPath = Application.persistentDataPath + "/AssetBundles/";
    /// <summary>
    /// 这个是游戏工程本地ab的路径（工程目录内的）
    /// </summary>
    //public static string DownLoadABPath = "file://" + Environment.CurrentDirectory.Replace("\\", "/")+"/StreamingAssets";

    /// <summary>
    /// 游戏资源解压到当前平台（Windows/IOS/Android etc.）的ab的存放路径（工程目录外的）
    /// </summary>
    //public static string OutDownLoadABPath = PersistentDataPath();//+ MainConst.AppName + "/";

    /// <summary>
    /// 各个路径下接的游戏名后缀
    /// </summary>
    public static string gamePostFix = "/" + MainConst.AppName + "/";

    //public static string serverURL = @"D:/tools/apache-tomcat-7.0.90/webapps/ROOT/";
    public static string serverURL = @"E:\tomcat\apache-tomcat-7.0.90\webapps/ROOT/".Replace("\\","/");
    
    public static string serverResPath = serverURL;// + GetPlatformName();

    public static string m_downloadAssetBundlesPath = "";

    private static string m_streamingAssetsPath = "";
    private static string m_assetBundlesPath = "";
    private static string m_localAssetBundlesPath = "";
    private static string m_persistentDataPath = "";
    private static string m_temporaryDataPath = "";
    private static string m_wwwDataPath = "";
    private static string m_removeDataPath = "";

    /// <summary>
    /// StreamingAssets 目录路径,各个平台的StreamingAssets路径
    /// //win平台读写文件时路径不需要加file:///，www操作的路径需要加file:///
    /// </summary>
//    public static string StreamingAssetsPath(bool isWinIOOperate=false)
//    {
//        //switch (Application.platform)
//        //{
//        //    case RuntimePlatform.Android:
//        //        path = "jar:file://" + Application.dataPath + "!/assets/";
//        //        break;
//        //    case RuntimePlatform.IPhonePlayer:
//        //        path = Application.dataPath + "/Raw/";
//        //        break;
//        //    default:
//        //        path = Application.streamingAssetsPath+"/";
//        //        break;
//        //}
//#if UNITY_ANDROID && !UNITY_EDITOR 
//        MainUtil.Log("android platform");
//        //m_streamingAssetsPath="jar:file://" + Application.dataPath + "!/assets/"; 
//        //m_streamingAssetsPath=Application.streamingAssetsPath+"/";
//        if (isWinIOOperate)
//        {
//            m_streamingAssetsPath = "jar:file://" + Application.dataPath + "!/assets/";
//        }
//        else
//        {
//            m_streamingAssetsPath = Application.streamingAssetsPath + "/";
//        }
//#elif UNITY_IOS && !UNITY_EDITOR
//        MainUtil.Log("iphone platform");
//        m_streamingAssetsPath= file:// + Application.dataPath +"/Raw/" 
//#else
//        if (isWinIOOperate)
//        {//win平台读写文件时路径不需要加file:///
//            m_streamingAssetsPath = Application.dataPath + "/StreamingAssets/";
//        }
//        else
//        {
//            m_streamingAssetsPath = "file:///" + Application.streamingAssetsPath + "/";
//        }
// #endif
//        return m_streamingAssetsPath;
//    }

    /// <summary>
    /// StreamingAssets 目录路径,各个平台的StreamingAssets路径
    /// //c# io 读写文件时路径不需要加file:///，www操作的路径需要加file:///
    /// 除了c#io的情况外的streamingassets都调用这个路径
    /// </summary>
    public static string StreamingAssetsPath()
    {
#if UNITY_ANDROID && !UNITY_EDITOR 
        //m_streamingAssetsPath = "file:///"+Application.streamingAssetsPath + "/";
        m_streamingAssetsPath = "jar:file://" + Application.dataPath + "!/assets/";
#elif UNITY_IOS && !UNITY_EDITOR
        m_streamingAssetsPath= file:// + Application.dataPath +"/Raw/" 
#else
        m_streamingAssetsPath = "file:///" + Application.streamingAssetsPath + "/";
#endif
        return m_streamingAssetsPath;
    }

    /// <summary>
    /// c# io流操作的streaming路径
    /// </summary>
    /// <returns></returns>
    public static string StreamingAssetsPath_IO()
    {
#if UNITY_ANDROID && !UNITY_EDITOR 
        m_streamingAssetsPath = Application.streamingAssetsPath + "/";
#elif UNITY_IOS && !UNITY_EDITOR
        m_streamingAssetsPath=  Application.dataPath +"/Raw/" 
#else
        //win平台读写文件时路径不需要加file:///
        m_streamingAssetsPath = Application.streamingAssetsPath + "/";
#endif
        return m_streamingAssetsPath;
    }

    /// <summary>
    /// 资源存放在StreamingAssets里，这里是各个平台的StreamingAssets路径
    /// </summary>
    //public static string AppContentPath()
    //{
    //    string path = string.Empty;
        
        
    //    //path = 
    //    //        #if UNITY_ANDROID && !UNITY_EDITOR 
    //    //            "jar:file:///" + Application.dataPath + "!/assets/"; 
    //    //        #elif UNITY_IOS && !UNITY_EDITOR
    //    //            file:// + Application.dataPath +"/Raw/" 
    //    //        #elif UNITY_STANDALONE_WIN || UNITY_EDITOR 
    //    //            "file://" + Application.dataPath + "/StreamingAssets/";
    //    //        #else 
    //    //            string.Empty; 
    //    //        #endif

    //    path =
    //                #if UNITY_ANDROID && !UNITY_EDITOR 
    //                    "jar:file://" + Application.dataPath + "!/assets/"; 
    //                #elif UNITY_IOS && !UNITY_EDITOR
    //                     file:// + Application.dataPath +"/Raw/" 
    //                #else
    //                    "file:///" + Application.dataPath + "/StreamingAssets/";
    //                //#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
    //                //#else 
    //                //                    string.Empty; 
    //                #endif
    //    return path;
    //}

    /// <summary>
    /// 放置可下载ab的服务器的地址
    /// </summary>
    public static string remoteAssetBundlesPath
    {
        get
        {
            if (m_removeDataPath == "")
            {
                m_removeDataPath = "http://192.168.1.101/borderExpansion/" + PlatformFolderForAssetBundles() + "/";
            }
            return m_removeDataPath;
        }
    }

    
//    /// <summary>
//    /// 各平台上（本地平台）的持久化数据存放目录
//    ///assetBundles resource path out of package Update from server
//    /// </summary>
//    public static string PersistentDataPath(bool isWinIOOperate=false)
//    {
//        if (Application.isMobilePlatform)
//        {
//            MainUtil.Log("mobile platform");
//            if (isWinIOOperate) {
//                m_persistentDataPath = Application.persistentDataPath + gamePostFix;
//            }
//            else {
//                m_persistentDataPath = "file://" + Application.persistentDataPath + gamePostFix;
//            }
//        }
//        else if (Application.platform == RuntimePlatform.OSXEditor)
//        {
//            MainUtil.Log("macos editor platform");
//            int i = Application.dataPath.LastIndexOf('/');
//            m_persistentDataPath = Application.dataPath.Substring(0, i + 1) + MainConst.AppName + "/";
//        }
//        else {
//            MainUtil.Log("win platform");
//            if (isWinIOOperate)//c#读写win本地文件的时候加上file://会无法识别
//            {
//                m_persistentDataPath = Application.persistentDataPath + gamePostFix;
//            }
//            else
//            {
//                m_persistentDataPath = "file:///" + Application.persistentDataPath + gamePostFix;// "file:///" + 
//            } 
//        }

////#if UNITY_ANDROID && !UNITY_EDITOR 
////                   //m_persistentDataPath= "file:///" + Application.persistentDataPath + gamePostFix;
////                   m_persistentDataPath= Application.persistentDataPath + gamePostFix;

////#elif UNITY_IOS && !UNITY_EDITOR
////                    m_persistentDataPath = Application.dataPath.Substring(0, i + 1) + MainConst.AppName + "/";
////#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
////        if (isWinIOOperate)//c#读写win本地文件的时候加上file://会无法识别
////        {
////            m_persistentDataPath = Application.persistentDataPath + gamePostFix;// "file:///" + 
////        } else
////        {
////            m_persistentDataPath = "file:///" + Application.persistentDataPath + gamePostFix;// "file:///" + 
////        }

////#else
////                    string.Empty; 
////#endif
//        return m_persistentDataPath;//如果这里返回的路径为""，则可以获得appName/ 这种解包路径，会解包到与Assets同级目录下
//    }




    /// <summary>
    /// 各平台上（本地平台）的持久化数据存放目录
    ///除了c#io的情况外的PersistentDataPath都调用这个路径,比如www加载
    /// </summary>
    public static string PersistentDataPath()
    {
#if UNITY_ANDROID
        m_persistentDataPath = "file:///" + Application.persistentDataPath + gamePostFix;
#elif UNITY_IOS
         int i = Application.dataPath.LastIndexOf('/');
         m_persistentDataPath = Application.dataPath.Substring(0, i + 1) + MainConst.AppName + "/";
#else
         m_persistentDataPath = "file:///" + Application.persistentDataPath + gamePostFix;// "file:///" + 
#endif
        return m_persistentDataPath;//如果这里返回的路径为""，则可以获得appName/ 这种解包路径，会解包到与Assets同级目录下
    }

    /// <summary>
    ///  c# io流操作的persistent路径
    /// </summary>
    /// <returns></returns>
    public static string PersistentDataPath_IO()
    {
#if UNITY_ANDROID 
        m_persistentDataPath = Application.persistentDataPath + gamePostFix;
#elif UNITY_IOS
         m_persistentDataPath = Application.dataPath.Substring(0, i + 1) + MainConst.AppName + "/";
#else
        //c#读写win本地文件的时候加上file://会无法识别
        m_persistentDataPath = Application.persistentDataPath + gamePostFix;
#endif
        return m_persistentDataPath;
    }



    /// <summary>
    /// 临时缓存目录
    /// </summary>
    public static string temporaryDataPath
    {
        get
        {
            if (m_temporaryDataPath == "")
            {
                m_temporaryDataPath = Application.temporaryCachePath + "/";
            }
            return m_temporaryDataPath;
        }
    }

    //public static string wwwDataPath
    //{
    //    get
    //    {
    //        if (m_wwwDataPath == "")
    //        {
    //            string updateServerUrl;
    //            if (GameConfigParams.hasParam(GameConfigParams.KEY_UpdateServer))
    //                updateServerUrl = GameConfigParams.getParam(GameConfigParams.KEY_UpdateServer);
    //            else
    //                updateServerUrl = "";
    //            m_wwwDataPath = updateServerUrl + ConfigPath.PlatformFolderForAssetBundles() + "/";
    //        }
    //        return m_wwwDataPath;
    //    }
    //}
    //public static string wwwApkPath
    //{
    //    get
    //    {
    //        string path = "";
    //        if (GameConfigParams.hasParam(GameConfigParams.KEY_UpdateServer))
    //        {
    //            path = GameConfigParams.getParam(GameConfigParams.KEY_UpdateServer);
    //            path += "bin/android/";
    //            return path;
    //        }
    //        else
    //        {
    //            return "";
    //        }
    //    }
    //}

    public static string GetPlatformName()
    {
#if UNITY_EDITOR
        return GetPlatformFolderForAssetBundles(EditorUserBuildSettings.activeBuildTarget);
#else
            return GetPlatformFolderForAssetBundles(Application.platform);
#endif
    }

#if UNITY_EDITOR
    public static string GetPlatformFolderForAssetBundles(BuildTarget target)
    {
        switch (target)
        {
            case BuildTarget.Android:
                return "Android";
            case BuildTarget.iOS:
                return "IOS";
            // case BuildTarget.WebPlayer:
            //     return "WebPlayer";
            case BuildTarget.StandaloneWindows:
            case BuildTarget.StandaloneWindows64:
                return "Windows";
            case BuildTarget.StandaloneOSXIntel:
            case BuildTarget.StandaloneOSXIntel64:
            case BuildTarget.StandaloneOSX:
                return "OSX";
            // Add more build targets for your own.
            // If you add more targets, don't forget to add the same platforms to GetPlatformFolderForAssetBundles(RuntimePlatform) function.
            default:
                return null;
        }
    }
#endif

    public static string GetPlatformFolderForAssetBundles(RuntimePlatform platform)
    {
        switch (platform)
        {
            case RuntimePlatform.Android:
                return "Android";
            case RuntimePlatform.IPhonePlayer:
                return "IOS";
            // case RuntimePlatform.WindowsWebPlayer:
            // case RuntimePlatform.OSXWebPlayer:
            //     return "WebPlayer";
            case RuntimePlatform.WindowsPlayer:
                return "Windows";
            case RuntimePlatform.OSXPlayer:
                return "OSX";
            // Add more build platform for your own.
            // If you add more platforms, don't forget to add the same targets to GetPlatformFolderForAssetBundles(BuildTarget) function.
            default:
                return null;
        }
    }

    /// <summary>
    /// 获取不同平台的存放ab的文件夹名
    /// </summary>
    /// <returns></returns>
    public static string PlatformFolderForAssetBundles()
    {
#if UNITY_EDITOR
        return GetPlatformFolderForAssetBundles(EditorUserBuildSettings.activeBuildTarget);
#else
		return GetPlatformFolderForAssetBundles(Application.platform);
#endif
    }


}
